package com.stylefeng.guns.core.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class HttpClientUtil {

	private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);


	public static String get(String url){
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
		HttpGet get = new HttpGet(url);
		try {
			CloseableHttpResponse response = httpClient.execute(get);
			HttpEntity responseEntity = response.getEntity();
			String content = EntityUtils.toString(responseEntity);
			return content;
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	public static String post(String url, Map<String, String> params) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
		HttpPost post = HttpClientUtil.getHttpPost();
		CloseableHttpResponse response = null;
		Map<String,String> formparams = new HashMap<String,String>();
		try {
			if (!CollectionUtils.isEmpty(params)) {
				Set<Entry<String, String>> keys = params.entrySet();
				Iterator<Entry<String, String>> entry = keys.iterator();
				while (entry.hasNext()) {
					Entry<String,String> tempEntry = entry.next();
					formparams.put(tempEntry.getKey(), tempEntry.getValue());
				}
			}
			StringEntity requestEntity = new StringEntity(JacksonUtil.toJson(formparams),"utf-8");//解决中文乱码问题    
			requestEntity.setContentEncoding("UTF-8");    
			requestEntity.setContentType("application/json");    
			post.setEntity(requestEntity);
			response = httpClient.execute(post);
			HttpEntity responseEntity = response.getEntity();
			String content = EntityUtils.toString(responseEntity);
			logger.info("httpClient请求，url{},入参{},返回{}", url, params, content);
			System.out.println("content:" + content);
			EntityUtils.consume(responseEntity);
			return content;
		} catch (ClientProtocolException e) {
			logger.error("httpClient请求异常", e);
		} catch (IOException e) {
			logger.error("httpClient请求异常", e);
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				logger.error("httpClient请求异常", e);
			}
		}
		return null;
	}

	private static  HttpPost getHttpPost(){

		HttpPost post = new HttpPost();
		RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(100000).setConnectTimeout(100000)
				.setSocketTimeout(10000).build();
		post.setConfig(config);
		return post;
	}

	private static CloseableHttpClient getHttpClient(){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		return  httpClient;
	}

}
